package kr.hyosang.dkbs.kiosk.widget;

import java.util.Calendar;

import javafx.application.Platform;
import javafx.scene.control.Label;
import kr.hyosang.dkbs.kiosk.factory.StyleFactory;

public class DateTime extends WidgetBase {
	private static final int TICK = 10 * 1000;		//10초
	
	private long mLastUpdated = 0;
	
	private Label lblDate;
	private Label lblNoon;
	private Label lblTime;
	
	public DateTime(Label date, Label noon, Label time) {
		lblDate = date;
		lblNoon = noon;
		lblTime = time;
		
		lblDate.setStyle(StyleFactory.getDateStyle());
		lblTime.setStyle(StyleFactory.getTimeStyle());
		
		init();
	}

	public void init() {
		mTickThread.start();
	}
	
	private Thread mTickThread = new Thread() {
		@Override
		public void run() {
			while(true) {
				long now = System.currentTimeMillis();
				
				if((now - mLastUpdated) > TICK) {
					mLastUpdated = now;
					
					Platform.runLater(new Runnable() {
						@Override
						public void run() {
							Calendar c = Calendar.getInstance();
							
							String date = String.format("%4d년 %d월 %d일",
									c.get(Calendar.YEAR), c.get(Calendar.MONTH)+1, c.get(Calendar.DAY_OF_MONTH));
							String noon = (c.get(Calendar.AM_PM) == Calendar.AM) ? "오전" : "오후";
							String time = String.format("%02d시 %02d분",
									c.get(Calendar.HOUR), c.get(Calendar.MINUTE));
							
							lblDate.setText(date);
							lblNoon.setText(noon);
							lblTime.setText(time);
							
						}
					});
				}
				
				try {
					Thread.sleep(1000);
				}catch(Exception e) {
					e.printStackTrace();
				}
			}
		}
		
	};

	@Override
	protected String getTag() {
		return "DateTime";
	}
}
